/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manage_image;

import Manager_Image_Utils.ButtonCloseFullScreen;
import Manager_Image_Utils.Constants;
import Manager_Image_Utils.ImageThumbnail;
import Manager_Image_Utils.Library;
import Manager_Image_Utils.PopupMenu;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class FullScreen extends Window 
{
    private Constants c=new Constants();
    private ButtonCloseFullScreen btnClose;
    private JScrollPane fviewframe;
    JPanel fthumbnailbottom = new JPanel();
    private JScrollPane thumnail;
    private JPanel bottom=new JPanel();
    private JPanel jPviewframe=new JPanel();
    
    private float fzoom = 1;
    private int fangle = 0;
    private int fimgW, imgH;// 
    private int fthumainlindex;
    private int thumnail_X;
    private int thumainl_Y;
    int findex = 0;
    private int findexold = 0;
    private Dimension screenSize;
    private Color Bg=Color.black;
    private int WindownW,WindownH;
    
    private File fFilePath;
    private File fcurrentfile;
    private boolean fflag = false;
    private Thread fplay;
    ArrayList<File> ListFile=new ArrayList<File>();
    ArrayList<Thread> ArrayThread=new ArrayList<Thread>();
    int count = 0;
    public FullScreen(ArrayList<File> f)
    {
        super(new Frame());
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        WindownW=(int)Math.ceil(screenSize.getWidth());
        WindownH=(int)Math.ceil(screenSize.getHeight())-30;
        
        setLayout(null);
        
        btnClose = new ButtonCloseFullScreen(c.iconClosewindow.getImage(),c.iconClosewindow);
        btnClose.isClicked=false;
        btnClose.setBounds((int)Math.ceil(screenSize.getWidth()-100),0,100,100);
        btnClose.setOpaque(true);
        btnClose.setBackground(Color.black);
        btnClose.setBorder(null);
        
        btnClose.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) 
          {
//              for(Thread t:ArrayThread){
//                  t.interrupt();
//              }
              dispose();
               // System.exit(0);
          }
        });
        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                {
                    dispose();
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        DrawTaskBar();
        DrawScreen();
        DrawThumnail();
        // set for b
        setBackground(Bg);
        thumnail_X = this.fthumbnailbottom.getY();
        thumainl_Y = this.fthumbnailbottom.getX();

        imgH = Constants.img_H;
        fimgW = Constants.img_W;
        
        
        add(btnClose, 1, 0);
        setBounds(0,0,WindownW, WindownH);
        
        Thread drawall=new Thread(){
            public void run(){
                try {
                    ShowImageInToThumnail();
                } catch (IOException ex) {
                    Logger.getLogger(FullScreen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        drawall.start();
        ArrayThread.add(fplay);
    }

    public static void main(String[] args) throws IOException 
    {
          //new FullScreen(new File("E://img//")).setVisible(true);
    }
    protected void DrawScreen(){
        jPviewframe.setBackground(Bg);
        jPviewframe.setBorder(null);
        jPviewframe.setBounds(0, 0, WindownW, (int)Math.ceil(WindownH*0.8));
        fviewframe=new JScrollPane();
        fviewframe.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        fviewframe.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        fviewframe.setBounds(0, 0, WindownW, (int)Math.ceil(WindownH*0.80));
        fviewframe.setBorder(null);
        this.add(fviewframe);
    }
    protected void DrawThumnail(){
        
        fthumbnailbottom.setBackground(Bg);
        fthumbnailbottom.setBounds(0,(int)Math.ceil(WindownH*0.80), WindownW, (int)Math.ceil(WindownH*0.15));
        
        thumnail=new JScrollPane(fthumbnailbottom, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        thumnail.add(fthumbnailbottom);
        thumnail.setBounds(0,(int)Math.ceil(WindownH*0.80), WindownW, (int)Math.ceil(WindownH*0.15));
        add(thumnail);
    }
    protected void DrawTaskBar(){
        
        //middle screen
        int x=(int)Math.ceil(WindownW)/2;
        int y=WindownH-10;
        int currentx=x;
        int currenty=y;
        int delimiter=5;
        int btwidth=16;
        JButton btnplay=new JButton(c.icplay);
        btnplay.setBounds(currentx, currenty, btwidth, 16);
        btnplay.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!fflag) {
                    fplay = new Thread() {
                        public void run() {
                            while (fflag) {
                                Next();
                                try {
                                    if (fflag) {
                                        fplay.sleep(3000);
                                    }
                                } catch (InterruptedException ex) {
                                    //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        }
                    };
                    fplay.start();
                    fflag = true;
                }
            }
        });
        currentx=currentx+delimiter+btwidth;
        JButton btnnext=new JButton(c.icnext);
        btnnext.setBounds(currentx, currenty, btwidth, 16);
        btnnext.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Next();
            }
        });
        currentx=currentx+delimiter+btwidth;
        JButton btnRotateLeft=new JButton(c.icrotateleft);
        btnRotateLeft.setBounds(currentx, currenty, btwidth, 16);
        btnRotateLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RotateAntiClockwise();
            }
        });
        currentx=currentx+delimiter+btwidth;
        JButton btnRotateRight=new JButton(c.icrotateright);
        btnRotateRight.setBounds(currentx, currenty, btwidth, 16);
        btnRotateRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RotateClockwise();
            }
        });
        
        
        currentx=x;
        JButton btnstop=new JButton(c.icstop);
        btnstop.setBounds(currentx, currenty, btwidth, 16);
        btnstop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (fflag) {
                    fplay.interrupt();
                    fflag = false;
                }
            }
        });
        currentx=currentx-delimiter-btwidth;
        JButton btnpre=new JButton(c.icprivious);
        btnpre.setBounds(currentx, currenty, btwidth, 16);
        btnpre.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Previous();
            }
        });
        currentx=currentx-delimiter-btwidth;
        JButton btnzoomin=new JButton(c.iczoomin);
        btnzoomin.setBounds(currentx, currenty, btwidth, 16);
        btnzoomin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ZoomOut();
                
            }
        });
        currentx=currentx-delimiter-btwidth;
        JButton btnzoomout=new JButton(c.iczoomout);
        btnzoomout.setBounds(currentx, currenty, btwidth, 16);
        btnzoomout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ZoomIn();
            }
        });
        currentx=currentx-delimiter;
        bottom.add(btnRotateLeft);
        bottom.add(btnRotateRight);
        bottom.add(btnpre);
        bottom.add(btnstop);
        bottom.add(btnplay);
        bottom.add(btnnext);
        bottom.add(btnzoomout);
        bottom.add(btnzoomin);
        //bottom.setBackground(Bg);
        bottom.setBounds(0,(int)Math.ceil(WindownH*0.95), WindownW,(int)Math.ceil(WindownH*0.05));
        this.add(bottom);
    }
    
    protected void Next() {
        findexold = findex;
        findex = (findex == thumnail.getComponentCount() - 1) ? 0 : findex + 1;
        DrawImage();
    }

    protected void Previous() {
        findexold = findex;
        findex = findex == 0 ? thumnail.getComponentCount() - 1 : findex - 1;
        DrawImage();
    }

    protected void ZoomIn() {
        fzoom = (fzoom - Constants.scaleperonce);
        fzoom = fzoom < 0 ? fzoom * Constants.scaleperonce : fzoom;
        DrawImage();
    }

    protected void ZoomOut() {
        fzoom = Constants.scaleperonce + fzoom;
        DrawImage();
    }

    protected void RotateAntiClockwise() {
        fangle = fangle - Constants.rotate;
        DrawImage();
    }

    protected void RotateClockwise() {
        fangle = fangle + Constants.rotate;// < 0 ? (rotate - Constants.rotate) * -1 : rotate - Constants.rotate;
        DrawImage();
    }
    protected void DrawImage() 
    {
        if (fthumbnailbottom.getComponents().length == 0) {
            return;
        }
        final ImageThumbnail lb = (ImageThumbnail) (fthumbnailbottom.getComponent(findex));
        if (lb == null) {
            return;
        }
        fcurrentfile=lb.getF();
        
        ImageThumbnail lbold = (ImageThumbnail) fthumbnailbottom.getComponent(findexold);
        if(lbold==null)
            return;
        lbold.setBorder(Constants.fullscreenborderUnSelected);
        lb.setBorder(Constants.fullscreenborderSelected);
        thumnail.getHorizontalScrollBar().setValue(findex * lb.getWidth());
        BufferedImage iBI = null;
        try {
            iBI = ImageIO.read(lb.getF());
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        fimgW = iBI.getWidth();
        imgH = iBI.getHeight();

        BufferedImage imgnew = iBI;
        if (fzoom != 1) {
            imgnew = Library.Resize(iBI, fimgW, imgH, fzoom);
        } else if (fangle != 0) {
            imgnew = Library.Rotate(imgnew, fimgW, imgH, fangle);
        } else {
            imgnew = iBI;
        }
        JLabel a=new JLabel( new ImageIcon((Image) imgnew));
        a.setCursor(new Cursor(Cursor.HAND_CURSOR));
        a.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent ev) {
                if (ev.isPopupTrigger()) {
                    PopupMenu menutrip;
                    menutrip = new PopupMenu(lb.getF());
                    menutrip.show(ev.getComponent(), ev.getX(), ev.getY());
                }
            }

            public void mouseReleased(MouseEvent ev) {
                if (ev.isPopupTrigger()) {
                    PopupMenu menutrip;
                    menutrip = new PopupMenu(lb.getF());
                    menutrip.show(ev.getComponent(), ev.getX(), ev.getY());
                }
            }
        });
        jPviewframe.removeAll();
        jPviewframe.add(a);
        fviewframe.setViewportView(jPviewframe);
    }
    protected void ShowImageInToThumnail() throws IOException {

        int x = thumnail_X;
        int y = thumainl_Y;
        count = 0;
        for (File f : ListFile) {
            if (Library.isImage(f.getPath())) {
                ImageIcon ii = new ImageIcon(f.getPath());
                BufferedImage imgroot=ImageIO.read(f);
                //BufferedImage in = Library.scaleImage(Constants.thumnail_W, Constants.thumnail_H, ii);
                int W,H;
                float scalethum=1;
                float tile=ii.getIconWidth()>Constants.thumnail_W?
                        (float)(Constants.thumnail_W*1.0/ii.getIconWidth())
                        :(float)(ii.getIconWidth()*1.0/Constants.thumnail_W);
                W=(int)Math.ceil(ii.getIconWidth()*tile);
                H=(int)Math.ceil(ii.getIconHeight()*tile);
                BufferedImage in = Library.Resize(imgroot,W,H, 1);
                ImageThumbnail btnthumnail = new ImageThumbnail(f,new ImageIcon((Image) in));
                btnthumnail.setBounds(x, y, Constants.thumnail_W, Constants.thumnail_H);
                x = x + Constants.thumnail_W + Constants.split;
                btnthumnail.setBorder(null);
                btnthumnail.setName(count + "");
                if (count == findex)
                {
                    btnthumnail.setBorder(Constants.fullscreenborderSelected);
                } else {
                    btnthumnail.setBorder(Constants.fullscreenborderUnSelected);
                }
                count++;
                btnthumnail.setPreferredSize(new Dimension(Constants.thumnail_W, Constants.thumnail_H));
                btnthumnail.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        ImageThumbnail lb = (ImageThumbnail) e.getSource();
                        findexold = findex;
                        findex =Integer.parseInt(lb.getName());
                        DrawImage();
                    }
                });
                fthumbnailbottom.add(btnthumnail);
            }
        }
        thumnail.setViewportView(fthumbnailbottom);
        DrawImage();
    }
}